from xlh import MSMS

def readMsm(infile):
	# this function, given a msm file, parses the file and
	# returns an array of MSMS() instances,
	msmfile = open(infile, 'r')
	n = 0
	new = []
	while 1:
		line = msmfile.readline()	#read file line
		try:
			int(line[:1])			#is it a number? Stupid way of a python newbie
		except ValueError:			#no, it is a flag, do something
			if line[:5] == "BEGIN":
				new.append(MSMS())
			elif line[:7] == "PEPMASS":
				temp = line[8:]
				temp = float(temp)
				new[n].setMz(temp)
			elif line[:6] == "CHARGE":
				temp = line[7:8]
				temp = int(temp)
				new[n].setCharge(temp)
			elif line[:5] == "TITLE":
				temp = line[6:]
				new[n].setDescription(temp)
			elif line[:3] == "END":
				n = n + 1
			elif line[:3] == "SEA" or line[:3] == "REP":
				continue
			elif not line:
				break
		else:						#the line starts with a number, it's a spectrum
			new[n].addPeak(line.split())
		
	msmfile.close()
	return new



